<template>
	<view :data-theme="theme()" :class="theme() || ''">
		<view class="top_head pr" :style="'background-color: ' + bgcolor + ';'">
			<!-- #ifndef MP-WEIXIN -->
			<view class="state_top"></view>
			<!-- #endif -->
			<view class="head_top" :style="'height:' + topBarTop() + 'px;'"></view>
			<view class="d-b-c" :style="topBarHeight() == 0 ? '' : 'height:' + topBarHeight() + 'px;'">
				<view class="index_log">
					<text :style="'color:' + titleTextColor + ';'" v-if="title_type == 'text'">{{ title_name }}</text>
					<image v-else :src="toplogo || '/static/logo.png'" mode="heightFix"></image>
				</view>
				<view v-if="hide_search" class="flex-1"></view>
				<template v-else>
					<!-- #ifndef MP-WEIXIN -->
					<view class="d-s-c flex-1">
						<view class="top_search special" style="color: #999" @click.stop="showSearch = true">
							<text class="icon iconfont icon-sousuo"
								:style="searchIconTxt"></text>{{ $t('index.search_product') }}
						</view>
						<view class="top_search_right d-c-c" :style="rightSearch" @click.stop="scanQrcode"><text
								class="icon iconfont icon-saoyisao1"></text></view>
					</view>
					<!-- #endif -->
					<!-- #ifdef MP-WEIXIN -->
					<view class="d-s-c flex-1">
						<view class="top_search search-wx" :style="'height:' + topBarHeight() + 'px;color:#999;'"
							@click="showSearch = true">
							<text class="icon iconfont icon-sousuo"></text>{{ $t('index.search_product') }}
						</view>
						<view class="top_search_right" :style="'height:' +
							topBarHeight() +
							'px;margin-right: ' +
							topBarRight() +
							';'
							" @click="scanQrcode">
							<text class="icon iconfont icon-saoyisao1"></text>
						</view>
					</view>
					<!-- #endif -->
				</template>
			</view>
			<view class="nuter" v-if="openCategory.open">
				<navBar style="width: 100%" :color="openCategory.color" @currentIndex="setIndex" :currentI="thisindex"
					:navList="categoryList">
				</navBar>
			</view>
			<view style="height: 20rpx" v-else></view>
		</view>
		<view>
			<view v-if="thisindex == 0">
				<diy style="position: relative" :diyItems="items" :serviceUserId="serviceUserId" :nickName="nickName"
					ref="diyRef"></diy>
			</view>
			<view v-if="thisindex != 0">
				<view class="product-list">
					<view v-for="(pitem, pindex) in product_list" :key="pindex" class="product_item o-h"
						:class="pindex % 2 == 1 ? 'product_item_right' : ''" @click="
							gotoPage(
								'/pages/product/detail/detail?productId=' + pitem.productId
							)
							">
						<view>
							<image :src="pitem.productImage" mode="" style="width: 345rpx; height: 345rpx"></image>
						</view>
						<view class="text-ellipsis-2 f26 pro_name p-0-20 mt20">{{
							pitem.productName
						}}</view>
						<view class="mt36 mb20 p-0-20">
							<text class="f20 fb redF6">HK$</text>
							<text class="f32 fb redF6">{{ pitem.productPrice }}</text>
							<text class="text-d-line f20 gray9 ml10" v-if="pitem.linePrice > 0">HK$</text>
							<text class="text-d-line f24 gray9" v-if="pitem.linePrice > 0">{{ pitem.linePrice }}</text>
						</view>
					</view>
				</view>
				<view class="d-c-c p30 ww100" v-if="product_list.length == 0 && !loading">
					<text class="iconfont icon-wushuju"></text>
					<text class="cont">{{ $t('index.no_records') }}</text>
				</view>
				<uni-load-more v-else :loadingType="loadingType"></uni-load-more>
			</view>
		</view>
		<!-- </scroll-view> -->

		<!--{{$t('index.click_to_favorite')}}-->
		<view class="collection-box" v-if="is_collection" :style="'top:' + (topBarTop() + topBarHeight() + 10) + 'px;'">
			<view class="inner">
				<text> {{ $t('index.click') }}“</text>
				<text class="point">.</text>
				<text class="point point-big">.</text>
				<text class="point">.</text>
				<text>”{{ $t('index.add_to_app') }}\{{ $t('index.quick_access') }} </text>
			</view>
			<button type="primary" class="close-btn" @click="is_collection = false">
				x
			</button>
		</view>

		<!--{{$t('index.follow_official_account')}}-->
		<!-- #ifdef MP-WEIXIN -->
		<view class="follow-gzh" :class="isAuto ? 'isAuto' : ''" v-if="is_follow == '1'">
			<text class="icon iconfont icon-guanbi" @click="is_follow = 0"></text>
			<official-account></official-account>
		</view>
		<!-- #endif -->

		<!--{{$t('index.homepage_push')}}-->
		<Homepush v-if="is_homepush" :homepush_data="homepush_data"></Homepush>
		<searchProduct :isShow="showSearch" @close="closeSearch"></searchProduct>
		<tabBar></tabBar>
	</view>
</template>

<script>
import diy from "@/components/diy/diy.vue";
import Homepush from "./home-push/home-push.vue";
import navBar from "@/components/navBar/navBar.vue";
import uniLoadMore from "@/components/uni-load-more.vue";
import searchProduct from "@/components/searchProduct.vue";
import config from "@/env/config.js";
import { i18n } from "@/locale"
//#ifdef H5
import jweixin from "weixin-js-sdk";
//#endif

export default {
	components: {
		diy,
		Homepush,
		navBar,
		uniLoadMore,
		searchProduct,
	},
	data() {
		return {
			serviceUserId: 0,
			nickName: 0,
			loading: true,
			/*是否正在加载*/
			loadding: true,
			/*手机高度*/
			phoneHeight: 0,
			/*可滚动视图区域高度*/
			scrollviewHigh: 0,
			background: "",
			listData: [],
			indicatorDots: true,
			autoplay: true,
			interval: 2000,
			duration: 500,
			items: [],
			/*收藏引导*/
			is_collection: false,
			/*关注*/
			is_follow: "0",
			/*是否首页推送*/
			is_homepush: false,
			/*首页推送数据*/
			homepush_data: {},
			target: 0,
			thisindex: 0,
			loadingIndex: null,
			categoryList: [],
			product_list: [],
			page: 1,
			last_page: 0,
			no_more: false,
			title_name: "",
			title_type: "text",
			bgcolor: "",
			url: "",
			jweixin: null,
			toplogo: "",
			titleTextColor: "",
			diytop: 0,
			showSearch: false,
			openCategory: {
				color: "#000000",
				open: 0,
			},
			isAuto: 0,
			searchStyle: {},
			searchIconTxt: "",
			rightSearch: "",
			hide_search: false,
		};
	},
	watch: {
		thisindex: function (n, o) {
			if (n != o) {
				this.categoryId = this.categoryList[n].categoryId;
				this.toggleInit();
				if (n != 0) {
					this.getProduct();
				}
			}
		},
		bgcolor: function () {
			const colorRgb = this.bgcolor || "#ffffff";
			const activeList = ["#ffffff", "#FFFFFF"];
			const flag = activeList.includes(colorRgb);
			this.$nextTick(() => {
				this.searchStyle = {
					// height: `${this.topBarHeight()}px`,
					color: flag ? "#ccc" : colorRgb,
				};
				if (this.topBarHeight() && this.topBarHeight() > 0) {
					this.searchStyle.height = `${this.topBarHeight()}px`;
				}
				this.searchIconTxt = {
					color: flag ? "#ccc" : colorRgb,
				};
				this.rightSearch = {
					borderColor: flag ? "#ccc" : colorRgb,
				};
			});
		},
	},
	computed: {
		/*加载中状态*/
		loadingType() {
			if (this.loading) {
				return 1;
			} else {
				if (this.product_list.length != 0 && this.no_more) {
					return 2;
				} else {
					return 0;
				}
			}
		},
	},
	onShow() {
		
		uni.setNavigationBarTitle({
			title: this.$t('index.homepage'),
		});
		if (this.loadingIndex == 0) {
			this.$refs.diyRef.loadinData();
		}
		this.getTabBarLinks();
		this.getsign();

	},
	onTabItemTap() {
		//#ifdef H5
		if (process.env.NODE_ENV === "production") {
			window.location.href =
				config.app_url + config.h5_addr + "/pages/index/index";
		}
		//#endif
	},
	onReady() {
		uni.hideTabBar();
	},
	onLoad() {
		//#ifdef H5
		if (this.isWeixin()) {
			this.url = window.location.href;
		}
		//#endif
		this._freshing = false;
		/*获取订单列表*/
		this.getData();
		this.getList();
		// this.loadingIndex = null;
		// //#ifdef H5
		// if (this.isWeixin()) {
		//   this.url = window.location.href;
		// }
		// //#endif
		// this.getData();
		// this.getList();
		// this.getTabbar();
	},
	onPullDownRefresh() {
		if (this.thisindex == 0) {
			this.getData();
		} else {
			this.toggleInit();
			this.getProduct();
		}
	},
	onReachBottom() {
		this.scrolltolowerFunc();
	},
	mounted() {
		// this.init();
	},

	/*分享当前页面*/
	onShareAppMessage() {
		let self = this;
		return {
			title: self.title_name,
			path: "/pages/index/index?" + self.getShareUrlParams(),
		};
	},
	methods: {
		/*初始化*/
		// init() {
		//   let _this = this;
		//   uni.getSystemInfo({
		//     success(res) {
		//       _this.phoneHeight = res.windowHeight;
		//       let view = uni.createSelectorQuery().select(".top_head");
		//       view
		//         .boundingClientRect((data) => {
		//           let h = _this.phoneHeight - data.height;
		//           _this.scrollviewHigh = h;
		//         })
		//         .exec();
		//     },
		//   });
		// },
		// onRefresh() {},
		getsign() {
			let self = this;
			// #ifdef H5
			self._postBody(
				"index/getSignPackage", {
				url: self.url,
				paySource: self.getPlatform(),
			},
				function (res) {
					self.signPackages = res.data.signPackage;
					// 配置微信扫一扫参数
					//#ifdef H5
					if (self.url != "") {
						console.log(self.signPackages, "signpacages123");
						self.jweixin = self.configWxScan(self.signPackages);
					}
					//#endif
					uni.hideLoading();
				}
			);
			// #endif
		},
		/*获取首页分类*/
		getData() {
			let self = this;
			uni.showLoading({
				title: 'loading',
			});
			
			self._get(
				"index/index", {
				url: self.url,
				source: self.getPlatform(),
			},
				function (res) {
					self.loadingIndex = 0;
					let page = res.data.page;
					console.log('首页加载中',page)
					let auto =
						uni.getStorageSync("TabBar").isAuto &&
						uni.getStorageSync("TabBar").isAuto != 0;
					self.isAuto = auto;
					self.listData = res.data.list;
					self.background = res.data.background;
					self.items = page.items;
					self.title_name = page.page.params.shareTitle;
					self.bgcolor = page.page.style.titleBackgroundColor;
					self.setPage(page.page);
					self.toplogo = page.page.params.toplogo;
					self.title_type = page.page.params.titleType;
					self.titleTextColor = page.page.style.titleTextColor;
					self.openCategory = page.page.category;
					self.serviceUserId = res.data.service.serviceUserId;
					self.nickName = res.data.service.nickName;
					let plantServiceObj = {
						serviceUserId: self.serviceUserId,
						nickName: self.nickName,
					};
					uni.setStorageSync(
						"plantServiceObj",
						JSON.stringify(plantServiceObj)
					);
					//弹出收藏
					// #ifdef  MP-WEIXIN
					let isFirst = uni.getStorageSync("isFirst");
					if (isFirst == "" && res.data.setting.collection.status == "1") {
						self.is_collection = true;
						uni.setStorageSync("isFirst", 1);
					}
					self.is_follow = res.data.setting.officia.status;
					// #endif
					// 首页推送
					let homepush_name = uni.getStorageSync("homepush_name");
					if (
						res.data.setting.homepush.isOpen &&
						homepush_name != res.data.setting.homepush.name
					) {
						self.homepush_data = res.data.setting.homepush;
						self.is_homepush = true;
					}
					// 配置微信扫一扫参数
					//#ifdef H5
					//   if (self.url != "") {
					//     self.jweixin = self.configWxScan(self.signPackages);
					//     let params = {};
					//     self.configWx(self.signPackage, res.data.share.shareParams, params);
					//   }
					//#endif
					uni.hideLoading();
					self.loadding = false;
					uni.stopPullDownRefresh();
				},
				function(err){
					console.log('请求失败',err)
				}
			);
		},
		/*获取数据*/
		getList() {
			let self = this;
			self._get("product/category/index", {}, function (res) {
				self.categoryList = res.data.list;
				let fistIndex = {
					name: self.$t('index.recommend'),
					categoryId: "0",
				};
				self.categoryList.unshift(fistIndex);
			});
		},
		/*获取数据*/
		getProduct() {
			let self = this;
			let page = self.page;
			self.loading = true;
			self._postBody(
				"product/product/lists", {
				pageIndex: page || 1,
				categoryId: self.categoryId,
				search: "",
				sortType: "all",
				sortPrice: 0,
				pageSize: 20,
			},
				function (res) {
					self.loading = false;
					uni.stopPullDownRefresh();
					self.product_list = self.product_list.concat(res.data.records);
					self.last_page = res.data.lastPage;
					if (res.data.lastPage <= 1) {
						self.no_more = true;
					}
					// #ifdef MP
					if (page >= 9) {
						self.no_more = true;
					}
					// #endif
				}
			);
		},
		/*可滚动视图区域到底触发*/
		scrolltolowerFunc() {
			let self = this;
			if (self.page < self.last_page) {
				self.page++;
				self.getProduct();
			}
			self.no_more = true;
		},
		/*设置页面*/
		setPage(page) {
			uni.setNavigationBarTitle({
				title: page.params.name,
			});

			let colors = "#000000";
			if (page.style.titleTextColor == "white") {
				//字母要小写
				colors = "#ffffff";
			}
			uni.setNavigationBarColor({
				frontColor: colors,
				backgroundColor: "#ffffff",
			});
		},
		// 滑動切换触发的事件
		toggle(e) {
			let index = e.detail.current;
			this.target = index;
			this.categoryId = this.categoryList[index].categoryId;
			this.toggleInit();
			if (index != 0) {
				this.getProduct();
			}
		},
		toggleInit() {
			this.page = 1;
			this.last_page = 0;
			this.no_more = false;
			this.product_list = [];
		},
		scroll(e) {
			this.diytop = e.detail.scrollTop;
		},
		//点击nav控制下面的展示
		setIndex(e) {
			this.thisindex = e;
		},
		/* 禁止手动滑动 */
		stopTouchMove() {
			return true;
		},
		/*扫一扫核销*/
		scanQrcode: function () {
			let self = this;
			//#ifdef H5
			// 只允许通过相机扫码
			self.jweixin.scanQRCode({
				needResult: 1,
				scanType: ["qrCode", "barCode"],
				success: function (res) {
					self.gotoPage("/pages/store/clerkorder?orderNo=" + res.resultStr);
				},
				error: function (res) {
					uni.showToast({
						title: self.$t('index.scan_failed'),
					});
				},
			});
			//#endif
			//#ifndef H5
			// 只允许通过相机扫码
			uni.scanCode({
				onlyFromCamera: true,
				success: function (res) {
					if (res.errMsg == "scanCode:ok") {
						self.gotoPage("/pages/store/clerkorder?orderNo=" + res.result);
					} else {
						uni.showToast({
							title: self.$t('index.scan_failed'),
						});
					}
				},
			});
			//#endif
		},
		/* 关闭搜索 */
		closeSearch() {
			this.showSearch = false;
		},
	},
};
</script>

<style lang="scss">
$footer-tabBar-height: 50px;

.top_head {
	line-height: 30px;
	z-index: 1;
	padding: 0 26rpx;
	/* padding-left: 26rpx; */
	position: sticky;
	top: 0;
	left: 0;
	z-index: 100;
}

.head_top {
	width: 100%;
	height: var(--status-bar-height);
}

.index_log {
	// width: 130rpx;
	min-height: 48rpx;
	line-height: 48rpx;
	font-size: 26rpx;
	font-weight: 800;
	color: #ffffff;
	display: flex;
	justify-content: center;
	align-items: center;
}

.index_log image {
	width: 130rpx;
	height: 48rpx;
}

.top_search {
	/* width: 380rpx; */
	height: 60rpx;
	line-height: 60rpx;
	background: #f7f7f7;
	border-radius: 30rpx 0 0 30rpx;
	font-size: 24rpx;
	margin-left: 10rpx;
	color: #999;
}

.top_search.search-wx {
	// width: 300rpx;
	flex: 1;
}

.top_search.special {
	width: 100%;
	border-right: none;
	font-size: 24rpx;
}

.top_search_right {
	background: #f7f7f7;
	border-radius: 0 30rpx 30rpx 0;
	height: 60rpx;
	position: relative;
	border: none;
	box-sizing: border-box;
}

.top_search_right::before {
	content: " ";
	width: 1rpx;
	height: 20rpx;
	background-color: #999;
	left: 0;
	top: 0;
	bottom: 0;
	margin: auto;
	position: absolute;
}

.top_search_right .icon-saoyisao1 {
	padding: 0 20rpx;
	flex-shrink: 0;
	/* border-left: 1rpx solid #D9D9D9; */
}

.top_search .icon-sousuo {
	font-size: 30rpx;
	color: #999;
	margin-left: 20rpx;
	margin-right: 10rpx;
}

.top_bg {
	position: absolute;
	top: 0;
	z-index: 0;
	width: 750rpx;
	height: 350rpx;
}

.top_bg image {
	width: 750rpx;
	height: 350rpx;
}

.nuter {
	width: 100%;
	height: 80rpx;
	line-height: 80rpx;
	display: flex;
	justify-content: space-around;
	font-size: 35rpx;
	padding-bottom: 10rpx;
}

.nuter view {
	flex: 1;
	font-size: 30rpx;
	text-align: center;
	transition: all 0.5s ease 0.1s;
}

swiper {
	height: 80vh;
	width: 100%;
	overflow: hidden;
}

.swiper-item {
	overflow-y: scroll;
	width: 100%;
	height: 99%;
	box-sizing: border-box;
}

.banner {
	height: 240upx;
	width: 710upx;
}

.banner image {
	width: 710rpx;
	height: 240rpx;
}

.group-seckill .left .iconfont {
	margin-right: 8rpx;
	color: rgb(226, 35, 26);
	font-size: 40rpx;
}

.group-seckill .list .item {
	display: flex;
	flex-direction: column;
	justify-content: flex-start;
	align-items: center;
	width: 150rpx;
	height: 230rpx;
	border: 1px dashed #cccccc;
}

.group-seckill .list text {
	line-height: 60rpx;
	color: #e2231a;
}

.group-seckill .list image {
	width: 150rpx;
	height: 150rpx;
}

.group-hd .datetime .time {
	padding: 4rpx;
	background: #e2231a;
	color: #ffffff;
	border-radius: 4rpx;
}

.group-hd .datetime .point {
	padding: 0 10rpx;
	color: #e2231a;
}

.collection-box {
	position: fixed;
	width: 380rpx;
	padding: 20rpx;
	top: 20rpx;
	right: 20rpx;
	line-height: 40rpx;
	font-size: 24rpx;
	border-radius: 16rpx;
	background: #ffffff;
	border: 1px solid #eeeeee;
	box-shadow: 0 0 6rpx 0 rgba(0, 0, 0, 0.08);
	z-index: 100;
}

.collection-box::after {
	position: absolute;
	content: "";
	display: block;
	right: 140rpx;
	top: -15rpx;
	transform: rotate(45deg);
	width: 30rpx;
	height: 30rpx;
	transform: rotate;
	background: #ffffff;
	border-left: 1px solid #eeeeee;
	border-top: 1px solid #eeeeee;
}

.collection-box .point {
	width: 20rpx;
	height: 20rpx;
	font-size: 60rpx;
	line-height: 0;
	color: #666666;
}

.collection-box .point-big {
	font-size: 80rpx;
}

.collection-box .close-btn {
	position: absolute;
	padding: 0;
	right: 10rpx;
	top: 10rpx;
	width: 40rpx;
	height: 40rpx;
	line-height: 30rpx;
	background: #ffffff;
	color: #999999;
	border-radius: 50%;
}

.follow-gzh {
	position: fixed;
	left: 0;
	right: 0;
	bottom: calc($footer-tabBar-height + env(safe-area-inset-bottom));
	border-radius: 16rpx;
	box-shadow: 0 0 20rpx 0 rgba(0, 0, 0, 0.1);
	background: #ffffff;
	z-index: 10;
}

.follow-gzh .iconfont {
	display: block;
	position: absolute;
	right: 10rpx;
	top: 10rpx;
	z-index: 99;
}

.product-list {
	display: flex;
	justify-content: space-between;
	flex-wrap: wrap;
	align-items: center;
	position: relative;
	z-index: 1;
}

.product_item {
	width: 345rpx;
	margin: 20rpx;
	border-radius: 12rpx;
	background-color: #ffffff;
}

.product_item.product_item_right {
	margin-left: 0;
}

.product_item .pro_name {
	height: 68rpx;
	line-height: 34rpx;
}
</style>